<?php
/**
 * 附件图片缩略图
 * 
 * @package AS_Thumbnail
 * @author Mr.Asong
 * @version 1.0.0
 * @link http://mrasong.com
 *
 *
 */
class ASThumbnail_Plugin implements Typecho_Plugin_Interface
{
	const TYPE_SMALL  = 'small';
	const TYPE_MIDDLE = 'middle';
	const TYPE_LARGE  = 'large';
	const SIZE_SMALL  = 200;
	const SIZE_MIDDLE = 800;
	const SIZE_LARGE  = 1600;
	
	private $type = ''; //类型   small/middle/large
	private $path = ''; //原文件路径
	
	public static function getExts(){
		return array(
			'jpg'  => '.jpg ',
			'jpeg' => '.jpeg ',
			'gif'  => '.gif ',
			'png'  => '.png '
		);
	}
	
	public static function getSize($type){
		$size = array(
			self::TYPE_SMALL => array(
				'90'  => '90px',
				'96'  => '96px',
				'100' => '100px',
				'120' => '120px',
				'128' => '128px',
				'160' => '160px',
				'180' => '180px',
				'200' => '200px'
			),
			self::TYPE_MIDDLE => array(
				'600' => '600px',
				'640' => '640px',
				'680' => '680px',
				'720' => '720px',
				'760' => '760px',
				'800' => '800px'
			),
			self::TYPE_LARGE => array(
				'1200' => '1200px',
				'1280' => '1280px',
				'1440' => '1440px',
				'1600' => '1600px'
			)
		);
		return $size[$type];
	}
	
	
    /**
     * 激活插件方法,如果激活失败,直接抛出异常
     * 
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function activate()
    {
		Typecho_Plugin::factory('Widget_Upload')->upload = array(__CLASS__, 'createHandler');
		Typecho_Plugin::factory('Widget_Upload')->modify = array(__CLASS__, 'createHandler');
    }
    
    /**
     * 禁用插件方法,如果禁用失败,直接抛出异常
     * 
     * @static
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function deactivate(){ }
    
    /**
     * 获取插件配置面板
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form 配置面板
     * @return void
     */
    public static function config(Typecho_Widget_Helper_Form $form){
		
		$allowed_ext =  new Typecho_Widget_Helper_Form_Element_Checkbox('allowed_ext', self::getExts(), array('jpg', 'jpeg', 'gif', 'png'),
			_t('允许生成缩略图的后缀') );
		$form->addInput($allowed_ext);	
		
		/** 小图 */
		self::addItem($form, self::TYPE_SMALL, self::SIZE_SMALL, '小图');
		
		/** 中图 */
		self::addItem($form, self::TYPE_MIDDLE, self::SIZE_MIDDLE, '中图');
		
		/** 大图 */
		self::addItem($form, self::TYPE_LARGE, self::SIZE_LARGE, '大图');

		
	}
    
    /**
     * 个人用户的配置面板
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form
     * @return void
     */
    public static function personalConfig(Typecho_Widget_Helper_Form $form){}
	
    
    /**
     * 添加表单
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form
     * @return void
     */
	public static function addItem($form, $type, $default, $tips){
		$allowed =  new Typecho_Widget_Helper_Form_Element_Radio('allow_'.$type, array('1'=>'是', '0'=>'否'), '1',
			_t('是否生成'.$tips), _t('如：<b>'. date('YmdHis') .'.jpg</b> 生成后：<b>'. date('YmdHis') .'_'.$type.'.jpg</b>'));
		$form->addInput($allowed);
		
        $width = new Typecho_Widget_Helper_Form_Element_Select('w_'.$type, self::getSize($type), $default,
			_t($tips.'宽度') );
        $form->addInput($width);
		
        $height = new Typecho_Widget_Helper_Form_Element_Select('h_'.$type, self::getSize($type), $default,
			_t($tips.'高度') );
        $form->addInput($height);
	}
	
	
	/* 过滤格式 */
	public static function getType($type = NULL){
		if( $type == self::TYPE_SMALL || $type == self::TYPE_MIDDLE || $type == self::TYPE_LARGE ){
			return $type;
		}else if( $type == NULL ){
			return self::TYPE_SMALL;
		}
		return false ;
	}
	
	/* 获取新path */
	public static function getNewPath($path, $type = NULL){
		$ext = strtolower(strrchr($path, '.'));
		$suffix = self::getType($type);
		return str_replace($ext, '_'.$suffix.$ext, $path);
	}
	
	public static function getUrl($path, $type = NULL){
		$options = Typecho_Widget::widget('Widget_Options');
		$path_new = self::getNewPath($path, $type);
		if( $type == 'error' ){
			Typecho_Common::url($path, $options->siteUrl);
		}
		return Typecho_Common::url($path_new, $options->siteUrl);
	}
	
    /**
     * 生成缩略图入口
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form
     * @return void
     */
	public function createHandler($obj){
		$path = $obj->attachment->path;
		if( !$obj->attachment->isImage ){
			return false;
		}
		file_put_contents('test.txt', serialize($obj->attachment).PHP_EOL . self::getUrl($obj->attachment->path, 'middle') );
	}
	
    /**
     * 删除缩略图
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form
     * @return void
     */
	public function deleteHandler($arr){
	
	}
}
